package com.iwdnb.gkgz.common.utils;

import java.math.BigDecimal;
import java.util.List;

import com.iwdnb.gkgz.common.model.dto.StockDayData;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

@Slf4j
public class StockAverageUtils {

    /**
     * 是否回踩到均线
     *
     * @param code
     * @param stockDayData
     * @param yesterdayData
     * @param stockDayDataList
     * @return
     */
    public static StepBackAverage getStepBack(String code, StockDayData stockDayData, StockDayData yesterdayData,
        List<StockDayData> stockDayDataList, int period) {
        BigDecimal startPrice = stockDayData.getClosePrice();
        BigDecimal lowPrice = BigDecimalUtils.isGt(stockDayData.getOpenPrice(), yesterdayData.getClosePrice())
            ? yesterdayData.getClosePrice() : stockDayData.getOpenPrice();
        BigDecimal closePriceBefore = yesterdayData.getClosePrice();
        BigDecimal avgBefore = StockUtils.getAveragePrice(yesterdayData, period);
        BigDecimal rateBefore = BigDecimalUtils.subStractAndDividePrecent(closePriceBefore, avgBefore);
        BigDecimal avg = null;
        BigDecimal rate = null;
        BigDecimal closePrice = null;
        BigDecimal ONE = BigDecimal.ONE;
        BigDecimal NEGATIVE_ONE = new BigDecimal(-1);

        BigDecimal ZERO_FIVE = new BigDecimal(0.5);
        boolean fallBackFlag = false;
        String fallBackDate = null;
        String upBackDate = null;
        int fallBackIndex = -1;
        int upBackIndex = -1;
        BigDecimal fallBackPrice = null;
        BigDecimal upBackPrice = null;
        int size = stockDayDataList.size();
        for (int i = 0; i < size; i++) {
            StockDayData data = stockDayDataList.get(i);
            closePrice = data.getClosePrice();
            avg = StockUtils.getAveragePrice(data, period);
            rate = BigDecimalUtils.subStractAndDividePrecent(closePrice, avg);
            //回踩均线
            if (BigDecimalUtils.isGe(rateBefore, ONE)
                && BigDecimalUtils.isLe(rate, ZERO_FIVE)
                && BigDecimalUtils.isLe(closePrice, startPrice)
                && BigDecimalUtils.isGe(closePrice, lowPrice)
                && !fallBackFlag) {
                fallBackFlag = true;
                fallBackDate = data.getDate();
                fallBackIndex = i;
                fallBackPrice = data.getClosePrice();
                continue;
            }
            //重新站上均线
            if (fallBackFlag && BigDecimalUtils.isGe(rate, BigDecimal.ZERO)) {
                upBackDate = data.getDate();
                upBackIndex = i;
                upBackPrice = data.getClosePrice();
                break;
            }
            avgBefore = StockUtils.getAveragePrice(data, period);
            rateBefore = BigDecimalUtils.subStractAndDividePrecent(data.getClosePrice(), avgBefore);
        }
        StepBackAverage stepBackAverage = new StepBackAverage();
        stepBackAverage.setCode(code);
        stepBackAverage.setFallBackFlag(fallBackFlag);
        stepBackAverage.setFallBackDate(fallBackDate);
        stepBackAverage.setFallBackPrice(fallBackPrice);
        if (StringUtils.isNotBlank(upBackDate)) {
            stepBackAverage.setInterval(upBackIndex - fallBackIndex);
            stepBackAverage.setUpBackDate(upBackDate);
            stepBackAverage.setUpBackPrice(upBackPrice);
        }
        return stepBackAverage;
    }

    @Data
    public static class StepBackAverage {
        private String code;
        private boolean fallBackFlag;
        private String fallBackDate;
        private BigDecimal fallBackPrice;
        private String upBackDate;
        private BigDecimal upBackPrice;
        private Integer interval;
    }

}
